<link rel="stylesheet" href="/static/css/fileinput.css">

<form method=“post” role="form" enctype="multipart/form-data" style='padding: 15px;'>
    <div class="row">
        <div class="form-group">
            <label class="control-label">项目名称：</label>
            <label class="control-label" id="add" onclick="addProject()" style="margin-left: 269px;"><a  class="btn btn-success margin-bottom-15 margin-left-15"  ><i class="icon wb-add-file" aria-hidden="true"></i>新建项目</a></label>
            <label class="control-label"  id="input1" ><input type="text"  class="form-control" id="project"  /></label>
            <label class="control-label"  id="save" onclick="save1()"><button type="button" class="btn btn-outline btn-primary"><i class="icon wb-plus" aria-hidden="true"></i>保存</button></label>
            <label class="control-label"  id="del" onclick="del()" ><button type="button" class="btn btn-outline btn-primary"><i class="icon wb-home" aria-hidden="true"></i>取消</button></label>
            <select class="form-control" id="prjId" name="prjId" data-plugin="select2" style="width: 100%">
            </select>
        </div>
        <div class="form-group">
            <label class="control-label">文件描述：</label>
            <textarea class="form-control" name="subject" style="width: 100%;height: 50px;resize: none"></textarea>
        </div>
        <div class="form-group">
            <label class="control-label">选择文件：</label>
            <div class="input-group" style="width: 100%">
                <input id="file" name="file" type="file">
            </div>
        </div>
    </div>

</form>
<div class="modal-footer">
    <button class="btn btn-primary add-top-menu" onclick="uploadFile()">上传</button>
    <a class="btn btn-default" data-dismiss="modal" href="#">取消</a>
</div>
<div class="ui small modal signbox" id="regBox">
    <div class="modal-dialog" role="document">

    </div>
</div>
<script src="/static/js/fileinput.js"></script>

<script type="text/javascript">
    function uploadFile() {
        $("#file").fileinput("upload");
    }
    function setSelect() {
        $.ajax({
            url: "#springUrl('/file/listProject')",
            data: {"center": ""},
            type: "POST",
            success: function (text) {
                var d = JSON.parse(text);
                if (d.success && d.data) {
                    var options1="<option value=''></option>";
                    $("#prjId").append(options1);
                    for(var i=0;i<d.data.prjList.length;i++){
                        var name=d.data.prjList[i].name;
                        var id=d.data.prjList[i].id;
                        var options="<option value='"+id+"'>"+name+"</option>";
                        $("#prjId").append(options);
                    }
                }
            }
        });
    }
    $(function () {
        del();//隐藏
        setSelect();//设置项目下拉框值
        $("#prjId").select2({placeholder: '请选择'}).val(null).trigger("change");
        $("#file").fileinput({
            showUpload: false,
            maxFileCount: 1,
            uploadUrl: "/file/upload",  //上传地址
            showPreview: false,
            uploadAsync: true,
            mainClass: "input-group-lg"
        }).on('filepreupload', function (event, data, id, index) {
            data.form.append("subject", $("#subject").val());
            data.form.append("prjId", $("#prjId").val());
            data.form.append("fileType", '$!{fileType}');
        }).on("fileuploaded", function (event, data) {
                if (data.response.success) {
                    toastr.success("上传成功!");
                    $("#dialog").modal('hide');
                    reloadData();
                } else {
                    toastr.warning(data.response.message, "上传失败");
                }
            }
        );
        $('.kv-upload-progress').parent().append("<br/>");
        $('.kv-upload-progress').appendTo($('.kv-upload-progress').parent());
    });
    function addProject() {
        $("#add").hide();
        $("#input1").show();
        $("#save").show();
        $("#del").show();
    }
    function del() {
        $("#add").show();
        $("#input1").hide();
        $("#save").hide();
        $("#del").hide();
    }
    function save1() {
        var prj=$("#project").val();
        if(prj==""){
            alert("项目不能为空");
            return false;
        }
        $.ajax({
            url: "#springUrl('/sys/project/save1')",
            data: {"project": prj,"type":1},
            type: "POST",
            success: function (text) {
                var d = JSON.parse(text);
                if (d.success && d.data) {
                    toastr.success("保存成功!", "消息提示");
                    setSelect();
                    del();
                    setPrjs();
                }else {
                    toastr.warning("操作失败!");
                }
            }
        });
    }

</script>